//
//  Stack.swift
//  DataStructures
//
//  Created by Red App on 2018/6/13.
//  Copyright © 2018年 CEC-CESEC. All rights reserved.
//

import Foundation

public struct Stack<Element: Hashable> {
    private var storage: [Element] = []
    public init() {}
    
    public mutating func push(_ element: Element) {
        storage.append(element)
    }
    
    @discardableResult
    public mutating func pop() -> Element? {
        return storage.popLast()
    }
    
    public func peek() -> Element? {
        return storage.last
    }
    
    public var isEmpty: Bool {
        return peek() == nil
    }
}


extension Stack: CustomDebugStringConvertible {
    public var debugDescription: String {
        let topDivider = "----top----\n"
        let bottomDivider = "\n-----------"
        
        let stackElements = storage
            .map { "\($0)" }
            .reversed()
            .joined(separator: "\n")
        return topDivider + stackElements + bottomDivider
    }
}

extension Stack: ExpressibleByArrayLiteral {
    public typealias ArrayLiteralElement = Element

    public init(arrayLiteral elements: Stack.ArrayLiteralElement...) {
        storage = elements
    }
}


//extension Stack: ExpressibleByDictionaryLiteral {
//    public typealias Key = <#type#>
//    
//    public typealias Value = <#type#>
//    
//    public init(dictionaryLiteral elements: (Stack.Key, Self.Value)...) {
//        <#code#>
//    }
//}
